home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C17 / ReprocessHTML.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.6 KB  |  92 lines

  1. //: C17:ReprocessHTML.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Take Word's html output and fix up 
  7. // the code listings and html tags
  8. #include "../require.h"
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. using namespace std;
  13.  
  14. // Produce a new string which is the original
  15. // string with the html paragraph break marks
  16. // stripped off:
  17. string stripPBreaks(string s) {
  18.   int br;
  19.   while((br = s.find("<P>")) != string::npos)
  20.     s.erase(br, strlen("<P>"));
  21.   while((br = s.find("</P>")) != string::npos)
  22.     s.erase(br, strlen("</P>"));
  23.   return s;
  24. }
  25.  
  26. // After the beginning of a code listing is
  27. // detected, this function cleans up the listing
  28. // until the end marker is found. The first line
  29. // of the listing is passed in by the caller, 
  30. // which detects the start marker in the line.
  31. void fixupCodeListing(istream& in, 
  32.   ostream& out, string& line, int tag) {
  33.   out << line.substr(0, tag)
  34.     << "<PRE>" // Means "preformatted" in html
  35.     << stripPBreaks(line.substr(tag)) << endl;
  36.   string s;
  37.   while(getline(in, s)) {
  38.     int endtag = s.find("/""/""/"":~");
  39.     if(endtag != string::npos) {
  40.       endtag += strlen("/""/""/"":~");
  41.       string before = s.substr(0, endtag);
  42.       string after = s.substr(endtag);
  43.       out << stripPBreaks(before) << "</PRE>" 
  44.         << after << endl;
  45.       return;
  46.     }
  47.     out << stripPBreaks(s) << endl;
  48.   }
  49. }
  50.  
  51. string removals[] = {
  52.   "<FONT SIZE=2>",
  53.   "<FONT SIZE=1>",
  54.   "<FONT FACE=\"Times\" SIZE=1>",
  55.   "<FONT FACE=\"Times\" SIZE=2>",
  56.   "<FONT FACE=\"Courier\" SIZE=1>",
  57.   "SIZE=1", // Eliminate all other '1' & '2' size
  58.   "SIZE=2",
  59. };
  60. const int rmsz = 
  61.   sizeof(removals)/sizeof(*removals);
  62.  
  63. int main(int argc, char* argv[]) {
  64.   requireArgs(argc, 2);
  65.   ifstream in(argv[1]);
  66.   assure(in, argv[1]);
  67.   ofstream out(argv[2]);
  68.   string line;
  69.   while(getline(in, line)) {
  70.     // The "Body" tag only appears once:
  71.     if(line.find("<BODY") != string::npos) {
  72.       out << "<BODY BGCOLOR=\"#FFFFFF\" "
  73.       "TEXT=\"#000000\">" << endl;
  74.       continue; // Get next line
  75.     }
  76.     // Eliminate each of the removals strings:
  77.     for(int i = 0; i < rmsz; i++) {
  78.       int find = line.find(removals[i]);
  79.       if(find != string::npos)
  80.         line.erase(find, removals[i].size());
  81.     }
  82.     int tag1 = line.find("/""/"":");
  83.     int tag2 = line.find("/""*"":");
  84.     if(tag1 != string::npos)
  85.       fixupCodeListing(in, out, line, tag1);
  86.     else if(tag2 != string::npos)
  87.       fixupCodeListing(in, out, line, tag2);
  88.     else
  89.       out << line << endl;
  90.   }
  91. } ///:~
  92.